GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data.
安裝 django graphenepip install "graphene-django>=2.0"
創建新的子專案 graphAPIpython manage.py startapp graphAPI
至總管理專案 djangoAPI 新增路由 (urls)
from django.conf.urls import url, include
from django.contrib import admin
# 新增以下兩行 -------------------------------
from graphene_django.views import GraphQLView
from graphAPI.schema import schema
# -------------------------------------------
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include('api.urls')),
# 新增以下 -------------------------------
url(r'^graph/', GraphQLView.as_view(graphiql=True, schema=schema))
# ---------------------------------------
]
至子專案 graphAPI 創建檔案 schema.py (我們晚點再回來處理它)
至總管理專案 djangoAPI 新增設定 (settings)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
# 'corsheaders',
# 新增以下 -----------------------
'graphAPI',
'graphene_django'
#--------------------------------
]
# 新增以下 -----------------------
GRAPHENE = {
'SCHEMA': 'graphAPI.schema' # Where your Graphene schema lives
}
#--------------------------------
至此就完成主要設定囉 ^-^